package com.jxw.launcher.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import com.jxw.adapter.GridViewAdapter;

/* loaded from: classes.dex */
public class DragGridView extends GridView {
    private static final float AMP_FACTOR = 1.2f;
    private static final int DRAG_IMG_NOT_SHOW = 0;
    private static final int DRAG_IMG_SHOW = 1;
    private static final String LOG_TAG = "DragGridView";
    private int downRawX;
    private int downRawY;
    private ImageView dragImageView;
    private WindowManager.LayoutParams dragImageViewParams;
    private ImageView icon_app;
    private boolean isViewOnDrag;
    private TextView name_app;
    private AdapterView.OnItemLongClickListener onLongClickListener;
    private int preDraggedOverPositon;
    private WindowManager windowManager;

    public DragGridView(Context context) {
        super(context);
        this.isViewOnDrag = false;
        this.preDraggedOverPositon = -1;
        this.onLongClickListener = new AdapterView.OnItemLongClickListener() { // from class: com.jxw.launcher.view.DragGridView.1
            @Override // android.widget.AdapterView.OnItemLongClickListener
            public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long j) {
                GridViewAdapter gridViewAdapter = (GridViewAdapter) DragGridView.this.getAdapter();
                if (gridViewAdapter.getEditApp()) {
                    DragGridView.this.preDraggedOverPositon = i;
                    view.destroyDrawingCache();
                    view.setDrawingCacheEnabled(true);
                    Bitmap createBitmap = Bitmap.createBitmap(view.getDrawingCache());
                    DragGridView.this.dragImageViewParams.gravity = 51;
                    DragGridView.this.dragImageViewParams.width = (int) (createBitmap.getWidth() * DragGridView.AMP_FACTOR);
                    DragGridView.this.dragImageViewParams.height = (int) (createBitmap.getHeight() * DragGridView.AMP_FACTOR);
                    DragGridView.this.dragImageViewParams.x = DragGridView.this.downRawX - (DragGridView.this.dragImageViewParams.width / 2);
                    DragGridView.this.dragImageViewParams.y = DragGridView.this.downRawY - (DragGridView.this.dragImageViewParams.height / 2);
                    DragGridView.this.dragImageViewParams.flags = 408;
                    DragGridView.this.dragImageViewParams.format = -3;
                    DragGridView.this.dragImageViewParams.windowAnimations = 0;
                    if (((Integer) DragGridView.this.dragImageView.getTag()).intValue() == 1) {
                        DragGridView.this.windowManager.removeView(DragGridView.this.dragImageView);
                        DragGridView.this.dragImageView.setTag(0);
                    }
                    DragGridView.this.dragImageView.setImageBitmap(createBitmap);
                    DragGridView.this.windowManager.addView(DragGridView.this.dragImageView, DragGridView.this.dragImageViewParams);
                    DragGridView.this.dragImageView.setTag(1);
                    DragGridView.this.isViewOnDrag = true;
                    gridViewAdapter.hideView(i);
                    return true;
                }
                return true;
            }
        };
        initView();
    }

    public DragGridView(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
        this.isViewOnDrag = false;
        this.preDraggedOverPositon = -1;
        this.onLongClickListener = new AdapterView.OnItemLongClickListener() { // from class: com.jxw.launcher.view.DragGridView.1
            @Override // android.widget.AdapterView.OnItemLongClickListener
            public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long j) {
                GridViewAdapter gridViewAdapter = (GridViewAdapter) DragGridView.this.getAdapter();
                if (gridViewAdapter.getEditApp()) {
                    DragGridView.this.preDraggedOverPositon = i;
                    view.destroyDrawingCache();
                    view.setDrawingCacheEnabled(true);
                    Bitmap createBitmap = Bitmap.createBitmap(view.getDrawingCache());
                    DragGridView.this.dragImageViewParams.gravity = 51;
                    DragGridView.this.dragImageViewParams.width = (int) (createBitmap.getWidth() * DragGridView.AMP_FACTOR);
                    DragGridView.this.dragImageViewParams.height = (int) (createBitmap.getHeight() * DragGridView.AMP_FACTOR);
                    DragGridView.this.dragImageViewParams.x = DragGridView.this.downRawX - (DragGridView.this.dragImageViewParams.width / 2);
                    DragGridView.this.dragImageViewParams.y = DragGridView.this.downRawY - (DragGridView.this.dragImageViewParams.height / 2);
                    DragGridView.this.dragImageViewParams.flags = 408;
                    DragGridView.this.dragImageViewParams.format = -3;
                    DragGridView.this.dragImageViewParams.windowAnimations = 0;
                    if (((Integer) DragGridView.this.dragImageView.getTag()).intValue() == 1) {
                        DragGridView.this.windowManager.removeView(DragGridView.this.dragImageView);
                        DragGridView.this.dragImageView.setTag(0);
                    }
                    DragGridView.this.dragImageView.setImageBitmap(createBitmap);
                    DragGridView.this.windowManager.addView(DragGridView.this.dragImageView, DragGridView.this.dragImageViewParams);
                    DragGridView.this.dragImageView.setTag(1);
                    DragGridView.this.isViewOnDrag = true;
                    gridViewAdapter.hideView(i);
                    return true;
                }
                return true;
            }
        };
        initView();
    }

    public DragGridView(Context context, AttributeSet attributeSet, int i) {
        super(context, attributeSet, i);
        this.isViewOnDrag = false;
        this.preDraggedOverPositon = -1;
        this.onLongClickListener = new AdapterView.OnItemLongClickListener() { // from class: com.jxw.launcher.view.DragGridView.1
            @Override // android.widget.AdapterView.OnItemLongClickListener
            public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i2, long j) {
                GridViewAdapter gridViewAdapter = (GridViewAdapter) DragGridView.this.getAdapter();
                if (gridViewAdapter.getEditApp()) {
                    DragGridView.this.preDraggedOverPositon = i2;
                    view.destroyDrawingCache();
                    view.setDrawingCacheEnabled(true);
                    Bitmap createBitmap = Bitmap.createBitmap(view.getDrawingCache());
                    DragGridView.this.dragImageViewParams.gravity = 51;
                    DragGridView.this.dragImageViewParams.width = (int) (createBitmap.getWidth() * DragGridView.AMP_FACTOR);
                    DragGridView.this.dragImageViewParams.height = (int) (createBitmap.getHeight() * DragGridView.AMP_FACTOR);
                    DragGridView.this.dragImageViewParams.x = DragGridView.this.downRawX - (DragGridView.this.dragImageViewParams.width / 2);
                    DragGridView.this.dragImageViewParams.y = DragGridView.this.downRawY - (DragGridView.this.dragImageViewParams.height / 2);
                    DragGridView.this.dragImageViewParams.flags = 408;
                    DragGridView.this.dragImageViewParams.format = -3;
                    DragGridView.this.dragImageViewParams.windowAnimations = 0;
                    if (((Integer) DragGridView.this.dragImageView.getTag()).intValue() == 1) {
                        DragGridView.this.windowManager.removeView(DragGridView.this.dragImageView);
                        DragGridView.this.dragImageView.setTag(0);
                    }
                    DragGridView.this.dragImageView.setImageBitmap(createBitmap);
                    DragGridView.this.windowManager.addView(DragGridView.this.dragImageView, DragGridView.this.dragImageViewParams);
                    DragGridView.this.dragImageView.setTag(1);
                    DragGridView.this.isViewOnDrag = true;
                    gridViewAdapter.hideView(i2);
                    return true;
                }
                return true;
            }
        };
        initView();
    }

    public void initView() {
        setOnItemLongClickListener(this.onLongClickListener);
        this.dragImageView = new ImageView(getContext());
        this.dragImageView.setTag(0);
        this.dragImageViewParams = new WindowManager.LayoutParams();
        this.windowManager = (WindowManager) getContext().getSystemService("window");
    }

    @Override // android.widget.AbsListView, android.view.View
    public boolean onTouchEvent(MotionEvent motionEvent) {
        if (motionEvent.getAction() == 0) {
            this.downRawX = (int) motionEvent.getRawX();
            this.downRawY = (int) motionEvent.getRawY();
        } else if (motionEvent.getAction() == 2 && this.isViewOnDrag) {
            Log.i(LOG_TAG, "" + motionEvent.getRawX() + " " + motionEvent.getRawY());
            this.dragImageViewParams.x = (int) (motionEvent.getRawX() - ((float) (this.dragImageView.getWidth() / 2)));
            this.dragImageViewParams.y = (int) (motionEvent.getRawY() - ((float) (this.dragImageView.getHeight() / 2)));
            this.windowManager.updateViewLayout(this.dragImageView, this.dragImageViewParams);
            int pointToPosition = pointToPosition((int) motionEvent.getX(), (int) motionEvent.getY());
            if (pointToPosition != -1 && pointToPosition != this.preDraggedOverPositon) {
                ((GridViewAdapter) getAdapter()).swapView(this.preDraggedOverPositon, pointToPosition);
                this.preDraggedOverPositon = pointToPosition;
            }
        } else if (motionEvent.getAction() == 1 && this.isViewOnDrag) {
            ((GridViewAdapter) getAdapter()).showHideView();
            if (((Integer) this.dragImageView.getTag()).intValue() == 1) {
                this.windowManager.removeView(this.dragImageView);
                this.dragImageView.setTag(0);
            }
            this.isViewOnDrag = false;
        }
        return super.onTouchEvent(motionEvent);
    }
}
